#!/usr/bin/env python3
#
# FreeBSD VM image
#
# Copyright 2017-2019 Red Hat Inc.
#
# Authors:
#  Fam Zheng <famz@redhat.com>
#  Gerd Hoffmann <kraxel@redhat.com>
#
# This code is licensed under the GPL version 2 or later.  See
# the COPYING file in the top-level directory.
#

import os
import re
import sys
import time
import socket
import subprocess
import basevm

FREEBSD_CONFIG = {
    'cpu'	: "max,sse4.2=off",
}

class FreeBSDVM(basevm.BaseVM):
    name = "freebsd"
    arch = "x86_64"

    link = "https://download.freebsd.org/ftp/releases/ISO-IMAGES/12.2/FreeBSD-12.2-RELEASE-amd64-disc1.iso.xz"
    csum = "a4530246cafbf1dd42a9bd3ea441ca9a78a6a0cd070278cbdf63f3a6f803ecae"
    size = "20G"
    pkgs = [
        # build tools
        "git",
        "pkgconf",
        "bzip2",
        "python37",
        "ninja",

        # gnu tools
        "bash",
        "gmake",
        "gsed",
        "gettext",

        # libs: crypto
        "gnutls",

        # libs: images
        "jpeg-turbo",
        "png",

        # libs: ui
        "sdl2",
        "gtk3",
        "libxkbcommon",

        # libs: opengl
        "libepoxy",
        "mesa-libs",

        # libs: migration
        "zstd",
    ]

    # TODO: Enable gnutls again once FreeBSD's libtasn1 got fixed
    # See: https://gitlab.com/gnutls/libtasn1/-/merge_requests/71
    BUILD_SCRIPT = """
        set -e;
        rm -rf /home/qemu/qemu-test.*
        cd $(mktemp -d /home/qemu/qemu-test.XXXXXX);
        mkdir src build; cd src;
        tar -xf /dev/vtbd1;
        cd ../build
        ../src/configure --python=python3.7 --disable-gnutls {configure_opts};
        gmake --output-sync -j{jobs} {target} {verbose};
    """

    def console_boot_serial(self):
        self.console_wait_send("Autoboot", "3")
        self.console_wait_send("OK", "set console=comconsole\n")
        self.console_wait_send("OK", "boot\n")

    def build_image(self, img):
        self.print_step("Downloading install iso")
        cimg = self._download_with_cache(self.link, sha256sum=self.csum)
        img_tmp = img + ".tmp"
        iso = img + ".install.iso"
        iso_xz = iso + ".xz"

        self.print_step("Preparing iso and disk image")
        subprocess.check_call(["cp", "-f", cimg, iso_xz])
        subprocess.check_call(["xz", "-dvf", iso_xz])
        self.exec_qemu_img("create", "-f", "qcow2", img_tmp, self.size)

        self.print_step("Booting installer")
        self.boot(img_tmp, extra_args = [
            "-bios", "pc-bios/bios-256k.bin",
            "-machine", "graphics=off",
            "-device", "VGA",
            "-cdrom", iso
        ])
        self.console_init()
        self.console_boot_serial()
        self.console_wait_send("Console type",          "xterm\n")

        # pre-install configuration
        self.console_wait_send("Welcome",               "\n")
        self.console_wait_send("Keymap Selection",      "\n")
        self.console_wait_send("Set Hostname",          "freebsd\n")
        self.console_wait_send("Distribution Select",   "\n")
        self.console_wait_send("Partitioning",          "\n")
        self.console_wait_send("Partition",             "\n")
        self.console_wait_send("Scheme",                "\n")
        self.console_wait_send("Editor",                "f")
        self.console_wait_send("Confirmation",          "c")

        self.print_step("Installation started now, this will take a while")

        # post-install configuration
        self.console_wait("New Password:")
        self.console_send("%s\n" % self._config["root_pass"])
        self.console_wait("Retype New Password:")
        self.console_send("%s\n" % self._config["root_pass"])

        self.console_wait_send("Network Configuration", "\n")
        self.console_wait_send("IPv4",                  "y")
        self.console_wait_send("DHCP",                  "y")
        self.console_wait_send("IPv6",                  "n")
        self.console_wait_send("Resolver",              "\n")

        self.console_wait_send("Time Zone Selector",    "0\n")
        self.console_wait_send("Confirmation",          "y")
        self.console_wait_send("Time & Date",           "\n")
        self.console_wait_send("Time & Date",           "\n")

        self.console_wait_send("System Configuration",  "\n")
        self.console_wait_send("System Hardening",      "\n")

        # qemu user
        self.console_wait_send("Add User Accounts", "y")
        self.console_wait("Username")
        self.console_send("%s\n" % self._config["guest_user"])
        self.console_wait("Full name")
        self.console_send("%s\n" % self._config["guest_user"])
        self.console_wait_send("Uid",                   "\n")
        self.console_wait_send("Login group",           "\n")
        self.console_wait_send("Login group",           "\n")
        self.console_wait_send("Login class",           "\n")
        self.console_wait_send("Shell",                 "\n")
        self.console_wait_send("Home directory",        "\n")
        self.console_wait_send("Home directory perm",   "\n")
        self.console_wait_send("Use password",          "\n")
        self.console_wait_send("Use an empty password", "\n")
        self.console_wait_send("Use a random password", "\n")
        self.console_wait("Enter password:")
        self.console_send("%s\n" % self._config["guest_pass"])
        self.console_wait("Enter password again:")
        self.console_send("%s\n" % self._config["guest_pass"])
        self.console_wait_send("Lock out",              "\n")
        self.console_wait_send("OK",                    "yes\n")
        self.console_wait_send("Add another user",      "no\n")

        self.console_wait_send("Final Configuration",   "\n")
        self.console_wait_send("Manual Configuration",  "\n")
        self.console_wait_send("Complete",              "\n")

        self.print_step("Installation finished, rebooting")
        self.console_boot_serial()

        # setup qemu user
        prompt = "$"
        self.console_ssh_init(prompt, self._config["guest_user"], self._config["guest_pass"])
        self.console_wait_send(prompt, "exit\n")

        # setup root user
        prompt = "root@freebsd:~ #"
        self.console_ssh_init(prompt, "root", self._config["root_pass"])
        self.console_sshd_config(prompt)

        # setup serial console
        self.console_wait(prompt)
        self.console_send("echo 'console=comconsole' >> /boot/loader.conf\n")

        # setup boot delay
        self.console_wait(prompt)
        self.console_send("echo 'autoboot_delay=1' >> /boot/loader.conf\n")

        # setup virtio-blk #1 (tarfile)
        self.console_wait(prompt)
        self.console_send("echo 'chmod 666 /dev/vtbd1' >> /etc/rc.local\n")

        self.print_step("Configuration finished, rebooting")
        self.console_wait_send(prompt, "reboot\n")
        self.console_wait("login:")
        self.wait_ssh()

        self.print_step("Installing packages")
        self.ssh_root_check("pkg install -y %s\n" % " ".join(self.pkgs))

        # shutdown
        self.ssh_root(self.poweroff)
        self.console_wait("Uptime:")
        self.wait()

        if os.path.exists(img):
            os.remove(img)
        os.rename(img_tmp, img)
        os.remove(iso)
        self.print_step("All done")

if __name__ == "__main__":
    sys.exit(basevm.main(FreeBSDVM, config=FREEBSD_CONFIG))
